package com.edu.buservice.dal.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.IdUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.edu.buservice.dal.mapper.SysUsersMapper;
import com.edu.buservice.dal.model.SysUsers;
import com.edu.buservice.dal.service.SysUsersService;
import com.edu.buservice.util.model.TenantUtil;
import com.edu.buservice.util.model.enums.ErrorCodeEnum;
import com.edu.buservice.util.model.error.BizException;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.Objects;
import java.util.Optional;

/**
* @author 86177
* @description 针对表【sys_users(用户信息表)】的数据库操作Service实现
* @createDate 2025-02-05 23:34:19
*/
@Service
public class SysUsersServiceImpl extends ServiceImpl<SysUsersMapper, SysUsers> implements SysUsersService{


	@Override
	public List<SysUsers> getUserInfoByPhone(String phone) {

		if(TenantUtil.getTenantId() == null){
			return lambdaQuery()
					.eq(SysUsers::getPhone, phone)
					.eq(SysUsers::getDeleted, 0).list();
		}

		return lambdaQuery()
				.eq(SysUsers::getPhone, phone)
				.eq(SysUsers::getTenantId, TenantUtil.getTenantId())
				.eq(SysUsers::getDeleted, 0).list();

	}

	@Override
	public List<SysUsers> getUserInfoByUserIdOrPhone(String userId, String phone) {

		if (userId != null) {
			return lambdaQuery().eq(SysUsers::getUserId, userId).eq(SysUsers::getDeleted, 0).list();
		}
		
		if (phone != null) {
			return lambdaQuery().eq(SysUsers::getPhone, phone).eq(SysUsers::getDeleted, 0).list();
		}

		return List.of();
	}

	@Override
	public String addUserInfo(SysUsers sysUsers) {
		sysUsers.setId(null);
		sysUsers.setUserId(generateUserId());
		String s = Optional.ofNullable(sysUsers.getTenantId()).orElse(TenantUtil.getTenantId());
		sysUsers.setTenantId(s);
		sysUsers.setCreatedBy(sysUsers.getUserId());
		sysUsers.setUpdatedBy(sysUsers.getUserId());
		sysUsers.setCreatedAt(new Date());
		sysUsers.setUpdatedAt(new Date());
		save(sysUsers);
		return sysUsers.getUserId();
	}

	@Override
	public SysUsers getUserInfoById(String userId) {

		if (userId != null) {
			return lambdaQuery().eq(SysUsers::getUserId, userId).eq(SysUsers::getDeleted, 0).one();
		}

		return null;
	}

	@Override
	public List<SysUsers> getUserInfoByIds(List<String> userIds) {

		if(CollectionUtil.isEmpty(userIds)){
			return List.of();
		}

		return lambdaQuery().in(SysUsers::getUserId, userIds).eq(SysUsers::getDeleted, 0).list();

	}

	@Override
	public List<SysUsers> getUserInfoByCondition(SysUsers sysUsers) {

		LambdaQueryWrapper<SysUsers> lambdaQueryWrapper = new LambdaQueryWrapper<>();

		if (sysUsers.getUserId() != null) {
			lambdaQueryWrapper.eq(SysUsers::getUserId, sysUsers.getUserId());
		}

		if (sysUsers.getPhone() != null) {
			lambdaQueryWrapper.eq(SysUsers::getPhone, sysUsers.getPhone());
		}

		if (sysUsers.getUsername() != null){
			lambdaQueryWrapper.like(SysUsers::getUsername, sysUsers.getUsername());
		}

		if (sysUsers.getStatus() != null){
			lambdaQueryWrapper.eq(SysUsers::getStatus, sysUsers.getStatus());
		}

		if (sysUsers.getTenantId() != null){
			lambdaQueryWrapper.eq(SysUsers::getTenantId, sysUsers.getTenantId());
		}

		//限制最多查询20条数据
		lambdaQueryWrapper.last("limit 20");

		return list(lambdaQueryWrapper);

	}

	@Override
	public List<SysUsers> getUserInfoByTenantId(String tenantId) {

		if (tenantId != null) {
			return lambdaQuery().eq(SysUsers::getTenantId, tenantId).eq(SysUsers::getDeleted, 0).list();
		}

		return List.of();
	}

	@Override
	public boolean removeByUserId(List<String> userId) {

		if(CollectionUtil.isNotEmpty(userId)){
			return lambdaUpdate().in(SysUsers::getUserId, userId).remove();
		}


		return false;
	}

	@Override
	public boolean updateUserInfo(SysUsers sysUsers) {

		if (Objects.isNull(sysUsers)){
			throw new BizException(ErrorCodeEnum.PARAM_MISSING, "更新用户参数不能为空");
		}

		//用户Id为空，则抛出异常
		if (sysUsers.getUserId() == null){
			throw new BizException(ErrorCodeEnum.PARAM_MISSING, "更新用户参数不能为空");
		}

		LambdaUpdateWrapper<SysUsers> lambdaUpdateWrapper = new LambdaUpdateWrapper<>();

		String tenantId  = Optional.ofNullable(sysUsers.getTenantId()).orElse(TenantUtil.getTenantId());

		lambdaUpdateWrapper.eq(SysUsers::getUserId, sysUsers.getUserId());

		lambdaUpdateWrapper.eq(SysUsers::getTenantId, tenantId);

		if (StrUtil.isNotBlank(sysUsers.getPhone())){
			lambdaUpdateWrapper.set(SysUsers::getPhone, sysUsers.getPhone());
		}

		if (StrUtil.isNotBlank(sysUsers.getUsername())){
			lambdaUpdateWrapper.set(SysUsers::getUsername, sysUsers.getUsername());
		}

		if (StrUtil.isNotBlank(sysUsers.getAvatarUrl())){
			lambdaUpdateWrapper.set(SysUsers::getAvatarUrl, sysUsers.getAvatarUrl());
		}

		if (StrUtil.isNotBlank(sysUsers.getPassword())){
			lambdaUpdateWrapper.set(SysUsers::getPassword, sysUsers.getPassword());
		}

		if (StrUtil.isNotBlank(sysUsers.getEmail())){
			lambdaUpdateWrapper.set(SysUsers::getEmail, sysUsers.getEmail());
		}

		if (StrUtil.isNotBlank(sysUsers.getExtInfo())){
			lambdaUpdateWrapper.set(SysUsers::getExtInfo, sysUsers.getExtInfo());
		}

		if (sysUsers.getLastLoginIp() != null){
			lambdaUpdateWrapper.set(SysUsers::getLastLoginIp, sysUsers.getLastLoginIp());
		}

		if (sysUsers.getLastLoginTime() != null){
			lambdaUpdateWrapper.set(SysUsers::getLastLoginTime, sysUsers.getLastLoginTime());
		}



		if (sysUsers.getStatus() != null){
			lambdaUpdateWrapper.set(SysUsers::getStatus, sysUsers.getStatus());
		}


		if (sysUsers.getUpdatedAt() != null){
			lambdaUpdateWrapper.set(SysUsers::getUpdatedAt, sysUsers.getUpdatedAt());
		}

		if (sysUsers.getUpdatedBy() != null){
			lambdaUpdateWrapper.set(SysUsers::getUpdatedBy, sysUsers.getUpdatedBy());
		}

		if (sysUsers.getDeleted() != null){
			lambdaUpdateWrapper.set(SysUsers::getDeleted, sysUsers.getDeleted());
		}

		if (sysUsers.getCreatedAt() != null){
			lambdaUpdateWrapper.set(SysUsers::getCreatedAt, sysUsers.getCreatedAt());
		}

		if (sysUsers.getCreatedBy() != null){
			lambdaUpdateWrapper.set(SysUsers::getCreatedBy, sysUsers.getCreatedBy());
		}


		return update(lambdaUpdateWrapper);

	}

	//生成用户ID

	private String generateUserId() {

		return IdUtil.getSnowflakeNextIdStr();

	}


}




